// pages/editAddress/editAddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    milkArray: [{//投递奶箱
      id: 0,
      name: "需安装，工作人员和我联系"
    },
    {
      id: 1,
      name: "已安装奶箱"
    },
    {
      id: 2,
      name: "已有箱"
    },
    {
      id: 3,
      name: "信箱(位置电话联系)"
    },
    {
      id: 4,
      name: "报箱(位置电话联系)"
    },
    {
      id: 5,
      name: "传达室"
    },
    {
      id: 6,
      name: "不用装"
    }
    ],
    milkIndex: 0,
    submitBtn: false,//提交
    submitOne: false,//20200224提交一次
    redBg:false,
    text: '获取验证码', //按钮文字
    currentTime: 91, //倒计时
    disabled: false, //按钮是否禁用
  },
  staticData: {//提交
  },
  milkChange(e) {//选择
    this.setData({
      milkIndex: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  mrBtn: function () {//设置默认
    var mrBtn = this.data.mrBtn
    mrBtn = !mrBtn;
    this.setData({
      mrBtn: mrBtn
    })
  },
  name(e) {//收货人
    this.staticData.name = e.detail.value.replace(/\s+/g, '');
    this.submitText()
  },
  tel(e) {//手机号
    this.staticData.tel = e.detail.value.replace(/\s+/g, '');
    this.submitText()
    this.telText()
  },
  address(e) {//请选择小区
    this.staticData.address = e.detail.value.replace(/\s+/g, '');
    this.submitText()
  },
  addressHome(e) {//请输门牌号等信息
    this.staticData.addressHome = e.detail.value.replace(/\s+/g, '');
    this.submitText()
  },

  beizhu(e) {//备注
    this.staticData.beizhu = e.detail.value.replace(/\s+/g, '');
  },
  verificationCode(e) {//验证码
    this.staticData.verificationCode = e.detail.value.replace(/\s+/g, '');
    this.submitText();
    
  },
  submitText: function () {
    if (this.staticData.name && this.staticData.tel && this.staticData.verificationCode && this.staticData.address && this.staticData.addressHome) {
      this.setData({
        submitBtn: true
      });
    } else {
      this.setData({
        submitBtn: false
      });
    }
  },
 telText: function () {
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(this.staticData.tel)) {
      this.setData({
        redBg: false,
      });
    } else {
      this.setData({
        redBg: true
      });
    }
  },
  submit: function () {
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!this.staticData.name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'loading',
        // image:'../images/details/1.png',
        duration: 500
      })
      return;
    } else
      if (!this.staticData.tel) {
        wx.showToast({
          title: '请输入手机号',
          icon: 'loading',
          // image:'../images/details/1.png',
          duration: 500
        })
        return;
      } else
        if (!myreg.test(this.staticData.tel)) {
          wx.showToast({
            title: '手机号有误',
            icon: 'loading',
            duration: 500
          })
          return false;
        } else
        if (!this.staticData.verificationCode) {
          wx.showToast({
            title: '请输入验证码',
            icon: 'loading',
            // image:'../images/details/1.png',
            duration: 500
          })
          return;
        } else
        if (!this.staticData.address) {
          wx.showToast({
            title: '请选择小区',
            icon: 'loading',
            // image:'../images/details/1.png',
            duration: 500
          })
          return;
        } else
          if (!this.staticData.addressHome) {
            wx.showToast({
              title: '请输入门牌号等信息',
              icon: 'loading',
              // image:'../images/details/1.png',
              duration: 500
            })
            return;
          }
          else {//20200224
            this.setData({
              submitOne: true
            });
          }
        },
  //获取验证码按钮事件
  bindButtonTap: function () {
    var that = this;
    var phone = that.staticData.tel;
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var currentTime = that.data.currentTime ;
    if (!this.staticData.tel) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'loading',
        // image:'../images/details/1.png',
        duration: 500
      })
      return;
    } else
      if (!myreg.test(this.staticData.tel)) {
        wx.showToast({
          title: '手机号有误',
          icon: 'loading',
          duration: 500
        })
        return false;
      }  else {
      //当手机号正确的时候提示用户短信验证码已经发送 并禁止按钮点击导致定时器多次触发的bug
      that.setData({
        disabled: true,
      })

      wx.showToast({
        title: '短信验证码已发送',
        icon: 'none',
        duration: 2000
      });

      //设置一分钟的倒计时
      var interval = setInterval(function () {
        currentTime--; //每执行一次让倒计时秒数减一
        that.setData({
          text:'重新获取('+ currentTime + ')s', //按钮文字变成倒计时对应秒数
          redBg: false,
          disabled: true,
        })

        //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获         取验证码的按钮恢复到初始化状态只改变按钮文字

        if (currentTime <= 0) {
          clearInterval(interval)
          that.setData({
            text: '重新获取',
            currentTime: 91,
            disabled: false,
            redBg: true
          })
        }
      }, 1000);
    };


  },
})